package com.tedu.api.d4_io.in;

import java.io.*;

/*
  本类用于测试文件的读取
 */
public class Test3_In {
    public static void main(String[] args) {
        method();//字节流的读取
        method2();//高效字节流读取
    }

    public static void method2() {
        BufferedInputStream in=null;
        //创建字节流读取对象 instantiated
        try {
/*            in = new BufferedInputStream(
                    new FileInputStream(new File("D:\\ready\\1.txt")));*/
            in = new BufferedInputStream(
                    new FileInputStream("D:\\ready\\1.txt"));
            //读取
            int a;//in.read()只读取一次 只读取一次！！！！
            while((a=in.read())!=-1){
                System.out.println(a);
            }

        } catch (IOException e) {
            e.printStackTrace();//打印错误信息
        }finally {//finally一定会执行到的代码块
            //释放资源
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void method() {
        InputStream in=null;
        //创建字节流读取对象 instantiated
        try {
            in = new FileInputStream(new File("D:\\ready\\1.txt"));
            //读取一个字节...
/*          System.out.println(in.read());
            System.out.println(in.read());
            System.out.println(in.read());
            System.out.println(in.read());*/
            int a;
            while((a=in.read())!=-1){
                System.out.println(a);
            }


        } catch (IOException e) {
            e.printStackTrace();//打印错误信息
        }finally {//finally一定会执行到的代码块
            //释放资源
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
